function OnEvent_Initialize(){
	wmp.settings.autostart = 0;
	function wmp::CurrentPlaylistChange(){
		CachePlaylist();
		if(windows.playlist)
			LoadPlaylist();
		}
	function wmp::CurrentItemChange(){
		if(windows.player){
			info = -1;
			MsgPlus.CancelTimer("info");
			OnEvent_Timer("info");
			}
		}
	function wmp::PlayStateChange(NewState){
		switch(NewState){
			case 3:case 2:
			if(wsettings.nowplaying){
				var media = wmp.currentMedia;
				var tags = ["%t", "%ab", "%a", "%d", "%s"];
				var replace = ["{0}", "{2}", "{1}", media.duration?media.durationString:"Stream", [,, "Paused", "Playing"][NewState]];
				var mediastr = wsettings.nowplayingfmt;
				for(i in tags)
					mediastr = mediastr.replace(tags[i], replace[i]);
				SetNowPlaying(1, mediastr, media.getItemInfo("title"), media.getItemInfo("artist"), media.getItemInfo("WM/AlbumTitle"));
				}
			SetButton();
			break;
			case 1:case 10:
			if(wsettings.nowplaying)
				SetNowPlaying(0);
			SetButton();
			break;
			}
		}
	if(Messenger.MyStatus)
		OnEvent_Signin();
	}

function OnEvent_Signin(){
	wsettings = {
		backimage : "",
		language : "English.txt",
		nowplaying : true,
		nowplayingfmt : "%t - %a (%d)",
		style : "Default.xml",
		playeralpha : 255,
		playlistalpha : 255,
		volume : 50,
		loop : false,
		shuffle : false
		};
	
	var oCollection = wmp.playlistCollection, pname = "WLMini_" + Messenger.MyUserId;
	var oPlaylists = oCollection.getByName(pname);
	wmp.currentPlaylist = oPlaylists.count?oPlaylists.item(0):oCollection.newPlaylist(pname);
	
	try{
		var wsh = new ActiveXObject("WScript.Shell");
		var path = MsgPlus.ScriptRegPath + Messenger.MyUserId + "\\";
		
		for(i in wsettings)
			wsettings[i] = wsh.RegRead(path + i);
		
		wmp.settings.volume = wsettings.volume;
		wmp.settings.setMode("loop", wsettings.loop);
		wmp.settings.setMode("shuffle", wsettings.shuffle);
		} catch(e) {}
	
	LoadLanguage(wsettings.language);
	CachePlaylist();
	}

function OnEvent_Signout(){
	var wsh = new ActiveXObject("WScript.Shell");
	var path = MsgPlus.ScriptRegPath + Messenger.MyUserId + "\\";
	
	for(i in wsettings)
		wsh.RegWrite(path + i, wsettings[i]);
	
	wmp.close();
	}

function OnEvent_Uninitialize(){
	OnEvent_Signout();
	}

function OnPlaylistEvent_EditTextChanged(){
	MsgPlus.CancelTimer("search");
	MsgPlus.AddTimer("search", 300);
	}

function OnPlaylistEvent_LstViewDblClicked(PlusWnd, ControlId, ItemIdx){
	if(ItemIdx>-1)
		wmpfunc(3, playlist[PlusWnd.LstView_GetItemData("playlist", ItemIdx)]);
	}

function OnEvent_Timer(id){
	var media = wmp.currentMedia;
	switch(id){
		case "info":
		if(media)
			windows.player.SetControlText("info", media.getItemInfo(['title','WM/AlbumTitle','artist'][info+=info>1?-2:1]));
		MsgPlus.AddTimer("info", 3000);
		break;
		case "data":
		if(media){
			var PlusWnd = windows.player, wmpctrl = wmp.controls;
			PlusWnd.SetControlText("duration", media.duration?(wmpctrl.currentPositionString + "/" + media.durationString):"Stream");
			PlusWnd.SendControlMessage("position", 0x406, 1, (media.duration&0xFFFF)<<16);
			PlusWnd.SendControlMessage("position", 0x405, 1, wmpctrl.currentPosition);
			}
		MsgPlus.AddTimer("data", 500);
		break;
		case "search":LoadPlaylist();break;
		}
	}

function OnAboutEvent_Destroyed(){windows.about = 0;}
function OnConfigEvent_Destroyed(){windows.preferences = 0;}
function OnPlaylistEvent_Destroyed(){
	MsgPlus.CancelTimer("search");
	windows.playlist = 0;
	}
function OnPlayerEvent_Destroyed(){
	MsgPlus.CancelTimer("info");
	MsgPlus.CancelTimer("data");
	windows.player = 0;
	}

function OnPlayerEvent_CtrlClicked(PlusWnd, ControlId){
	switch(ControlId){
		case "stop":wmpfunc(1);break;
		case "pause":wmpfunc(2);break;
		case "play":wmpfunc(3);break;
		case "next":wmpfunc(4);break;
		case "previous":wmpfunc(5);break;
		case "loop":wmpfunc(6);break;
		case "shuffle":wmpfunc(7);break;
		case "playlist":ShowPlaylist();break;
		case "open":
		var ofd = OpenFileDialog(language['OFD_PLAYER_PLAY'], PlusWnd.handle, language['OFD_FILES_MUSIC'] + "|*.mp3;*.wma;*.wpl;*.m3u;*.wav|" + language['OFD_FILES_ALL'] + "|*.*|");
		if(ofd){
			var hPlaylist = wmp.currentPlaylist, iCount = hPlaylist.count;
			for(i in ofd)
				hPlaylist.appendItem(wmp.newMedia(ofd[i]));
			wmpfunc(3, iCount);
			}
		break;
		}
	}

function OnPlaylistEvent_CtrlClicked(PlusWnd, ControlId){
	if(ControlId == "AddPlaylist"){
		var ofd = OpenFileDialog(language['OFD_PLAYLIST_ADD'], PlusWnd.handle, language['OFD_FILES_MUSIC'] + "|*.mp3;*.wma;*.wpl;*.m3u;*.wav|" + language['OFD_FILES_ALL'] + "|*.*|");
		if(ofd){
			new hPlaylist = wmp.currentPlaylist;
			for(i in ofd)
				hPlaylist.appendItem(wmp.newMedia(ofd[i]));
			}
		return;
		}
	
	if(ControlId.substr(0,4) != "Base")
		new Function(ControlId + "()")();
	}

function OnEvent_MenuClicked(MenuItemId, Location, OriginWnd){
	new Function(MenuItemId + "()")();
	}

function OnPlayerEvent_MessageNotification(PlusWnd, Message, wParam, lParam){
	switch(Message){
		case 0x114:
		switch(lParam){
			case PlusWnd.GetControlHandle("position"):
			wmp.controls.currentPosition = PlusWnd.SendControlMessage("position", 0x400, 0, 0);
			break;
			case PlusWnd.GetControlHandle("volume"):
			wsettings.volume = wmp.settings.volume = PlusWnd.SendControlMessage("volume", 0x400, 0, 0);
			break;
			}
		break;
		case 0x233:
		var lpszFile = Interop.Allocate(10240);
		var i = Interop.Call("shell32", "DragQueryFileW", wParam, 0xFFFFFFFF, 0, 0);
		var hPlaylist = wmp.currentPlaylist, iCount = hPlaylist.count;
		for(x=0;x<i;x++){
			Interop.Call("shell32", "DragQueryFileW", wParam, x, lpszFile, 10240);
			hPlaylist.appendItem(wmp.newMedia(lpszFile.ReadString(0)));
			}
		if(lParam != 1337)
			wmpfunc(3, iCount);
		break;
		}
	}

function OnPlaylistEvent_MessageNotification(PlusWnd, Message, wParam, lParam){
	OnPlayerEvent_MessageNotification(PlusWnd, Message, wParam, 1337);
	}

function OnPlaylistEvent_LstViewRClicked(PlusWnd, ControlId, ItemIdx){
	if(ItemIdx > -1){
		var hMenu = Interop.Call("user32", "CreatePopupMenu");
		Interop.Call("user32", "AppendMenuW", hMenu, 0, 101, language['PLAYLIST_PLAY']);
		Interop.Call("user32", "AppendMenuW", hMenu, 0x800, 0, 0);
		Interop.Call("user32", "AppendMenuW", hMenu, 0, 102, language['PLAYLIST_MOVEUP']);
		Interop.Call("user32", "AppendMenuW", hMenu, 0, 103, language['PLAYLIST_MOVEDN']);
		Interop.Call("user32", "AppendMenuW", hMenu, 0x800, 0, 0);
		Interop.Call("user32", "AppendMenuW", hMenu, 0, 104, language['PLAYLIST_REMOVE']);
		Interop.Call("user32", "GetCursorPos", POINTAPI = Interop.Allocate(8));
		ProcessMenuItem(PlusWnd, ItemIdx, Interop.Call("user32", "TrackPopupMenu", hMenu, 0x0100, POINTAPI.ReadDWORD(0), POINTAPI.ReadDWORD(4), 0, PlusWnd.handle, 0));
		}
	}

function ProcessMenuItem(PlusWnd, ItemIdx, MenuId){
	if(MenuId){
		var iPLID = PlusWnd.LstView_GetItemData("playlist", ItemIdx);
		switch(MenuId){
			case 101:wmpfunc(3, iPLID);break;
			case 104:RemovePlaylist(wmp.currentPlaylist.item(iPLID));break;
			default:
			var hPlaylist = wmp.currentPlaylist, iCount = hPlaylist.count;
			switch(MenuId){
				case 102:hPlaylist.moveItem(iPLID, iPLID<1?iCount-1:iPLID-1);break;
				case 103:hPlaylist.moveItem(iPLID, iPLID==iCount-1?0:iPLID+1);break;
				}
			}
		}
	}